package com.suneee.store.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.suneee.context.page.Page;
import com.suneee.context.page.Sort;
import com.suneee.context.page.Sort.Order;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.store.bean.StockRecordDTO;
import com.suneee.store.dao.StockRecordDao;
import com.suneee.store.entity.StockRecord;

@Repository("stockRecordDao")
public class StockRecordDaoImpl extends GenericDao<StockRecord, String>implements StockRecordDao {

	@Override
	public int getDateTotal(String str) {
//		String sql = "select * from t_stock_record where record_id like ?";
//		Object[] args = new Object[1];
//		args[0] = str + "%";
//		WhereClauseBuilder builder = new WhereClauseBuilder();
//		builder.contains("record_id", str+"%");
		
		List<StockRecord> stockRecordList = this.selectWhere("record_id like ?", str+"%");
		if(stockRecordList == null)
			return 0;
		return stockRecordList.size();
	}

	@Override
	public Page<StockRecord> listView(StockRecordDTO dto) {
		// TODO Auto-generated method stub
		WhereClauseBuilder sbf = this.wrapWhereSQL(dto);
		return this.executeQuery(sbf.getSql(), dto, sbf.getArgs());
	}

	private WhereClauseBuilder wrapWhereSQL(StockRecordDTO dto){
		WhereClauseBuilder sbf = new WhereClauseBuilder(" select * from t_stock_record sr where 1=1 ");
		sbf.equals("sr.enterprise_code", dto.getEnterpriseCode());
		sbf.equals("sr.spu_id",dto.getSpuId());
		sbf.equals("sr.sku_id",dto.getSkuId());
		sbf.equals("sr.record_id",dto.getRecordId());
		sbf.equals("sr.record_type",dto.getRecordType());
		sbf.equals("sr.stock_type",dto.getStockType());
		sbf.equals("sr.operate_store",dto.getOperateStore());
		sbf.equals("sr.create_user_id", dto.getCreateUserId());
		sbf.between("sr.create_time", dto.getCreateTimeStart(), dto.getCreateTimeEnd());
		dto.setSort(new Sort("sr.create_time",Order.DESC));
		return sbf;
	}
}
